package com.example.NearByLocation;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2014/11/25.
 */
public class SecondActivity extends Activity {
    List<String> list;
    List<String> secondList=new ArrayList<String>();
    DataList dataList=new DataList();
    private Button classify_Button;
    private Button back_tofirst_bt;
    private TextView secondMenu_textview;
    private TextView classify_TextView;
    private ListView secondlist_listview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.secondmenu);

        SharedPreferences pref=getSharedPreferences("ServiceName", MODE_PRIVATE);
        String firstName=pref.getString("firstName", "aa");

        secondMenu_textview=(TextView) findViewById(R.id.secondMenu_textview);
        secondMenu_textview.setText(firstName);

        secondList=searchList(firstName);

        SecondAdapter secondAdapter=new SecondAdapter(secondList,R.layout.list_item,this);
        secondlist_listview =(ListView)findViewById(R.id.secondlist_listview);
        secondlist_listview.setAdapter(secondAdapter);

        back_tofirst_bt=(Button)findViewById(R.id.back_tofirst_bt);
        back_tofirst_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    private List<String> searchList(String str){
        list=new ArrayList<String>();
        if(str.equals("餐饮服务")){
            list=dataList.getCateringData();
        }else if(str.equals("购物服务")){
            list=dataList.getShoppingData();
        }
        else if(str.equals("生活服务")){
            list=dataList.getLifeServiceData();
        }
        else if(str.equals("体育休闲服务")){
            list=dataList.getSportingData();
        }
        else if(str.equals("住宿服务")){
            list=dataList.getResidentialData();
        }
        else if(str.equals("医疗保健服务")){
            list=dataList.getHealthCareData();
        }
        else if(str.equals("科教文化服务")){
            list=dataList.getS_EducationData();
        }
        else if(str.equals("交通设施服务")){
            list=dataList.getTrafficData();
        }
        return list;
    }
    class  SecondAdapter extends ListAdapter{
        SecondAdapter(List<String> list, int resource, Context context) {
            super(list, resource, context);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view= super.getView(position, convertView, parent);
            classify_TextView=(TextView)view.findViewById(R.id.classify_TextView);
            classify_Button=(Button)view.findViewById(R.id.classify_Button);
            final   String serviceName=  classify_TextView.getText().toString();
            classify_Button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    secondJump(serviceName);
                }
            });
            classify_TextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(SecondActivity.this,PoiListActivity.class);
                    SharedPreferences.Editor editor=getSharedPreferences("ServiceName",MODE_PRIVATE).edit();
                    editor.putString("thirdName",serviceName);
                    editor.commit();
                    startActivity(intent);
                }
            });
            return view;
        }
        private void  secondJump(String serviceName){
            SharedPreferences.Editor editor=getSharedPreferences("ServiceName",MODE_PRIVATE).edit();
            editor.putString("secondName",serviceName);
            editor.commit();
            Intent intent =new Intent(SecondActivity.this,ThirdActivity.class);
            startActivity(intent);
        }
    }
}
